<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$return['params']=array();
	if($_RQ['title']){
		$paramsg=array('uniacid'=>$_SESSION['uniacid']);
		$paramsg['title like']='%'.$_RQ['title'].'%';
		$goods=pdo_getall('jy_weishop_goods',$paramsg,array('id'),'id');
		if($goods){
			$goodsids=array_keys($goods);
		}else{
			$goodsids=-1;
		}
		$params['goodsid']=$goodsids;
		$return['params']['title']=$_RQ['title'];
	}
	$list=pdo_getslice('jy_mayitwc_goods',$params,array($pindex,$psize),$total,array(),'','id desc');
	foreach($list as &$item){
		$item['title']=pdo_getcolumn('jy_weishop_goods',array('id'=>$item['goodsid'],'uniacid'=>$_SESSION['uniacid']),'title');
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['htmls']=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'商品列表'),
			array('op'=>'edit','title'=>'添加上链商品'),
		),
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'id','title'=>'商品ID','type'=>'string'),
			array('name'=>'title','title'=>'商品名称','type'=>'string'),
			array('name'=>'status','type'=>'radio','title'=>'状态','options'=>'islian'),
		),'action'=>array(
			//array('type'=>'post','op'=>'up','title'=>'点击上链'),
		)),
		'search'=>array(
			array('type'=>'string','width'=>15,'title'=>'商品名称','name'=>'title'),
		),
	);
	$return['islian']=$this->islian;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id = intval($_RQ['id']);
	if($id){
		$return['item']=pdo_get('jy_mayitwc_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$return['item']=array();
	}
	$return['item']['biz_content']=json_decode($return['item']['biz_content'],true);
	$return['item']['response_data']=json_decode($return['item']['response_data'],true);
	$htmls=array(
			'type'=>'edit',
			'post'=>'post',
			'desc'=>'上链商品',
			'tabs'=>array(
				array('op'=>'list','title'=>'返回商品列表'),
			),
			'edit'=>array(
				array('name'=>'goodsid','title'=>'选择商品','type'=>'choose','action'=>'jy_weishop/goods.get_goods/'),
				array('type'=>'select2','title'=>'一级行业','name'=>'mainclass','options'=>'mainclass','name2'=>'subclass','title2'=>'二级行业','options2'=>'subclass'),
				array('name'=>'productBrand','title'=>'商品品牌','type'=>'string'),
				array('name'=>'productModel','title'=>'商品规格','type'=>'string'),
				array('name'=>'depositPrice','title'=>'免押金额','type'=>'unitstring','unit'=>'元'),
			)
	);
	$return['mainclass']=$this->mainclass;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
		$postdata=get_postdata();
		if(!$postdata['goodsid']){
			exi('请选择商品！','error');
		}
		if(!$postdata['mainclass'] || !$postdata['subclass']){
			exi('请选择一级行业，二级行业！','error');
		}
		$isadd=pdo_get('jy_mayitwc_goods',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$postdata['goodsid']));
		if($isadd['goodsid'] && $isadd['goodsid']!=$postdata['goodsid']){
			exi('该商品已经上过链！！','error');
		}
		if(!$this->setting['aliapp_id']){
			exi('请设置上链支付宝小程序（基础设置--选择上链支付宝小程序）！','error');
		}
		$data=array(
			'goodsid'=>$postdata['goodsid'],
			'mainclass'=>$postdata['mainclass'],
			'subclass'=>$postdata['subclass'],
			'productBrand'=>$postdata['productBrand'],
			'productModel'=>$postdata['productModel'],
			'depositPrice'=>$postdata['depositPrice'],
		);
		if(!$postdata['id']){
			$data['uniacid']=$_SESSION['uniacid'];
			$data['createtime']=TIMESTAMP;
			pdo_insert('jy_mayitwc_goods',$data);
			$postdata['id']=pdo_insertid();
		}else{
			pdo_update('jy_mayitwc_goods',$data,array('id'=>$postdata['id']));
		}
		$bizContent=$this->model('member')->goodsinfo($postdata['id'],$this->setting['aliapp_id']);
		$res=$this->model('model')->upinfo($bizContent,'product','',true);
		$update=array(
			'biz_content'=>ijson_encode($bizContent),
			'response_data'=>ijson_encode($res),
			'status'=>1,
			'uptime'=>TIMESTAMP
		);
		pdo_update('jy_mayitwc_goods',$update,array('id'=>$postdata['id']));
		exi('上链成功！');
}
